/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   ConfigDataHelper.cs
 * CreateData   :   2023/5/28 21:41:45
 * UnityVersion :   2021.3.20f1c1
 * Description  :   配置表获取助手
************************************************/

using System;
using Cysharp.Threading.Tasks;
using UnityEngine;

#pragma warning disable CS1998

namespace Main
{
    public static class ConfigDataHelper
    {
        private static ComponentIDE data;
        static AssetBundle configAB;
        private static async UniTask<ComponentIDE> GetData()
        {
            if (data == null)
            {
#if UNITY_EDITOR && AB_Local
                GameObject configObj = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(PathConfig.ConfigDataFilePath);
#else
                configAB = await AssetBundlesManager.Instance.LoadABAsync("configdata");
                GameObject configObj = await configAB.LoadAssetAsync<GameObject>("ConfigData") as GameObject;
#endif
                GameObject obj = GameObject.Instantiate(configObj);
                obj.hideFlags = HideFlags.HideInHierarchy;
                data = obj.GetComponent<ComponentIDE>();
            }
            return data;
        }
        /// <summary>
        /// 卸载包
        /// </summary>
        public static void Unload()
        {
            configAB?.Unload(false);
            GameObject.DestroyImmediate(data?.gameObject);
            data = null;
        }
        /// <summary>
        /// 获取配置表类型对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static async UniTask<T> Get<T>() where T : ScriptableObject
        {
            ComponentIDE ide = await GetData();
            return null;
        }
        /// <summary>
        /// 是否加载完成 AB 包 资源
        /// </summary>
        public static bool IsComplete
        {
            get
            {
                return data != null;
            }
        }
    }
}
